package com.hzdl.order.service.impl;

import com.hzdl.order.entity.Goods;
import com.hzdl.order.entity.Order;
import com.hzdl.order.service.GoodsService;
import com.hzdl.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class OrderServiceImpl implements OrderService {

    //初始化订单集合
    private static final Map<Integer, Order> orderMap = new HashMap<>();
    static {
        Order order1 = new Order();
        order1.setId(1);
        order1.setGoodsId(1);
        order1.setTime("2020-11-11 00:01");
        order1.setOrderNumber("ORD001");
        order1.setAmount(new BigDecimal("99.99"));
        order1.setCreateTime(LocalDateTime.now());

        Order order2 = new Order();
        order2.setId(2);
        order2.setGoodsId(2);
        order2.setTime("2020-11-11 00:02");
        order2.setOrderNumber("ORD002");
        order2.setAmount(new BigDecimal("199.99"));
        order2.setCreateTime(LocalDateTime.now());

        Order order3 = new Order();
        order3.setId(3);
        order3.setGoodsId(3);
        order3.setTime("2020-11-11 00:03");
        order3.setOrderNumber("ORD003");
        order3.setAmount(new BigDecimal("299.99"));
        order3.setCreateTime(LocalDateTime.now());

        orderMap.put(order1.getId(), order1);
        orderMap.put(order2.getId(), order2);
        orderMap.put(order3.getId(), order3);
    }

    /**
     * 服务之间调用： rest、fegin
     *
     * rest的url解析：
     *      http://微服务应用名/接口地址
     */
    //自动注入RestTemplate对象
//    @Autowired
//    private RestTemplate restTemplate;
    //调用商品微服务，根据商品ID查询商品详情
//    private Goods findGoodsById(Integer id){
//        //使用RestTemplate访问商品微服务的查询接口
//        ResponseEntity<Goods> responseEntity = restTemplate.getForEntity("http://goods/goods/one?id="+id, Goods.class);
//        return responseEntity.getBody();
//    }

    /**
       fegin调用商品微服务
     */
    @Autowired
    private GoodsService goodsService;

    @Override
    public List findAll() {
        //定义要返回的订单集合
        List orderList = new ArrayList();
        //遍历初始化的订单集合
        orderMap.forEach((k,v)->{
            Goods goods = null;
            try {
                goods = goodsService.findById(v.getGoodsId());
            } catch (Exception e) {
                e.printStackTrace(); // 打印详细异常
                goods = new Goods();
                goods.setId(v.getGoodsId());
                goods.setName("商品服务不可用");
                goods.setPrice(0.0);
            }
            //定义包含商品信息的订单信息
            HashMap order = new HashMap();
            order.put("订单ID",k);
            order.put("下单时间",v.getTime());
            order.put("商品ID",v.getGoodsId());
            order.put("商品名称",goods.getName());
            order.put("商品价格",goods.getPrice());
            //将订单信息添加进要返回的订单集合中
            orderList.add(order);
        });
        //返回订单集合
        return orderList;
    }
    @Override
    public Object findById(Integer id) {
        //获取相应ID的订单对象
        Order o = orderMap.get(id);
        Goods goods = null;
        try {
            goods = goodsService.findById(o.getGoodsId());
        } catch (Exception e) {
            e.printStackTrace(); // 打印详细异常
            goods = new Goods();
            goods.setId(o.getGoodsId());
            goods.setName("商品服务不可用");
            goods.setPrice(0.0);
        }
        //定义要返回的包含商品信息的订单信息
        HashMap order = new HashMap();
        order.put("订单ID",id);
        order.put("下单时间",o.getTime());
        order.put("商品ID",o.getGoodsId());
        order.put("商品名称",goods.getName());
        order.put("商品价格",goods.getPrice());
        //返回订单信息
        return order;
    }

    @Override
    public Order createOrder(Order order) {
        // 生成订单号
        order.setOrderNumber("ORD" + System.currentTimeMillis());
        // 设置创建时间
        order.setCreateTime(LocalDateTime.now());
        order.setTime(order.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")));
        
        // 如果使用 Map 存储
        order.setId(orderMap.size() + 1);
        orderMap.put(order.getId(), order);
        
        return order;
    }
}
